﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_App_Client.MainManagers
{
	#region using
    using DIS_App_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
    using DIS_App_Client.DIS_WS_BusinessLayer;
	#endregion
	public partial class FrmManageCategory : Form
	{
		public EventHandler Handler { get; set; }

		public FrmManageCategory(bool enableChon = false)
		{
			InitializeComponent();
			btnSelect.Enabled = enableChon;
		}

		private void FrmManageCategory_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnSelect_Click(object sender, EventArgs e)
		{
			var errorText = "";
			if (!ValidateInput(EnumUpdateMode.CHOOSING, ref errorText))
			{
				MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			var id = int.Parse(txtID.Text);
			var name = BusService.Instance.Category_SelectByID(id).Name;
			TransferDataInfo(this, new TransferSimpleDataEvent(id + "#" + name));
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void dtgvData_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (dtgvData.SelectedRows == null || dtgvData.SelectedRows.Count == 0)
				return;
			var selectedItem = dtgvData.SelectedRows[0];
			txtID.Text = dtgvData.SelectedRows[0].Cells["ID"].Value.ToString();
			var item = BusService.Instance.Category_SelectByID(int.Parse(txtID.Text));
			LoadDetailInfo(item);
		}

		public void LoadDetailInfo(CategoryDTO item)
		{
			txtName.Text = item.Name == null ? "" : item.Name.ToString();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/2))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Name",
				DataPropertyName = "Name",
				Name = "Name",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/2)) - 1
			};
			dtgvData.Columns.Add(objColumn);
		}


		private void LoadData()
		{
			var lstItem = BusService.Instance.Category_SelectAll();
			var dt = new DataTable();
			dt.Columns.Add("ID");
			dt.Columns.Add("Name");
			for (int i = 0; i < lstItem.Length; i++)
			{
				dt.Rows.Add(lstItem[i].ID, lstItem[i].Name);
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private bool ValidateInput(EnumUpdateMode mode, ref string errorText)
		{
            if (mode == EnumUpdateMode.CHOOSING)
            {
                if (txtID.Text == "")
                {
                    errorText = "Please select category";
                    return false;
                }
            }
            if (mode != EnumUpdateMode.DELETE)
            {
                if (txtName.Text == "")
                {
                    errorText = "Please input Name";
                    return false;
                }
            }
			return true;
		}

		public void TransferDataInfo(object sender, TransferSimpleDataEvent e)
		{
			this.Close();
			this.Handler(this, e);
		}

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtID.Text = "";
            txtName.Text = "";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var errorText = "";
            if (!ValidateInput(EnumUpdateMode.INSERT, ref errorText))
            {
                MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (ActionAdd())
            {
                MessageBox.Show(CommonHelper.GetMessage("Insert", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadData();
                btnReset_Click(sender, e);
            }
            else
            {
                MessageBox.Show(CommonHelper.GetMessage("Insert", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ActionAdd()
        {
            try
            {
                var item = new CategoryDTO
                {
                    Name = txtName.Text,
                };
                if (!BusService.Instance.Category_Insert(item))
                {
                    return false;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        private bool ActionDelete()
        {
            try
            {
                return BusService.Instance.Category_Delete(int.Parse(txtID.Text));
            }
            catch
            {
                return false;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var errorText = "";
            if (!ValidateInput(EnumUpdateMode.DELETE, ref errorText))
            {
                MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (MessageBox.Show(CommonHelper.GetMessage("Delete", "Confirm"), "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (ActionDelete())
                {
                    MessageBox.Show(CommonHelper.GetMessage("Delete", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadData();
                    btnReset_Click(sender, e);
                }
                else
                {
                    MessageBox.Show(CommonHelper.GetMessage("Delete", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private bool ActionUpdate()
        {
            try
            {
                var item = BusService.Instance.Category_SelectByID(int.Parse(txtID.Text));
                item.Name = txtName.Text;
                return BusService.Instance.Category_Update(item);
            }
            catch
            {
                return false;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            var errorText = "";
            if (!ValidateInput(EnumUpdateMode.UPDATE, ref errorText))
            {
                MessageBox.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (ActionUpdate())
            {
                MessageBox.Show(CommonHelper.GetMessage("Update", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadData();
            }
            else
            {
                MessageBox.Show(CommonHelper.GetMessage("Update", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

	}
}